/*
 * DoctorHash.java
 *
 * This program demonstrates the usage of a HashMap to store the different
 * Doctor objects.
 *
 * 
 */

package com.javacore.day11.lifeline;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * The Doctor class is created to store the doctor details for the
 * Doctor objects in a HashMap.
 * @author 
 */
public class DoctorHash {
  
  /** Declare a HashMap object. */
  static HashMap DoctorHashMap;
  
  /** Creates a new instance of DoctorHash */
  public DoctorHash() {
    DoctorHashMap = new HashMap();
  }
  
  /**
   * This method is invoked to add values to the HashMap.
   */
  void add() {
    
    // Declare and initialize variables to store user data
    String doctorName = "";
    String specialization = "";
    int hours = 0;
    String code = "";
    
    // Accept the user data
    Scanner input = new Scanner(System.in);
    
    System.out.println("Enter the Doctor code:");
    code = input.nextLine();
    
    System.out.println("Enter the name of the doctor:");
    doctorName = input.nextLine();
    
    System.out.println("Enter the specialization of the Doctor:");
    specialization = input.nextLine();
    
    System.out.println("Enter the availability of the Doctor in hours:");
    hours = input.nextInt();
    
    // Create an instance of DoctorDetails
    DoctorDetails doctorFields = new DoctorDetails(code, doctorName,
        specialization, hours);
    
    // Insert values into the HashMap
    DoctorHashMap.put(code, doctorFields);
  }
  
  /**
   * This method is invoked to display the doctor details based on the code
   * entered by the user as stored in the HashMap.
   */
  void display() {
    
    // Declare and initialize variables to store user data
    String searchCode = "";
    
    // Accept the user data
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the Doctor code:");
    searchCode = input.nextLine();
    
    if(DoctorHashMap.containsKey(searchCode)) {
      Object docObj = DoctorHashMap.get(searchCode);
      System.out.println(docObj);
    } else
      System.out.println("Doctor not found!!");
  }
  
  /**
   * This method is invoked to display the details of all the doctors as
   * stored in the HashMap.
   */
  void publish() {
    
    // Display the header information
    System.out.println( "The size of DoctorHashMap = " + DoctorHashMap.size());
    System.out.println( "The Doctors in the Panel are:" ) ;
    
    // Create a Set instance
    Set set= DoctorHashMap.keySet();
    
    // Create an Iterator instance
    Iterator iter = set.iterator();
    int i=1;
    while (iter.hasNext()) {
      System.out.println(" " + i + " ) " + DoctorHashMap.get(iter.next()));
      i++;
    }
  }
  
}
